<?php

namespace sffi\WechatRobotPlug\base;

use sffi\WechatRobotPlug\msg_attribute\{MsgContent, MsgEvent, MsgOther, MsgSend, MsgType, MsgUser};
use sffi\WechatRobotPlug\util\HttpUtil;

/**
 * @method atMsg($at, string $msg) 群聊at信息
 * @method fileMsg(string $path)   发送文件
 * @method cardMsg(string $content)发送名片
 * @method imageMsg(string $path)  发送图片
 * @method videoMsg(string $path)  发送视频
 * @method voiceMsg(string $path)  发送语音
 * @method musicMsg(string $title, string $desc, string $url, string $mp3url, string $imageUrl)  发送音乐
 * @method linkMsg(string $title, string $desc, string $url, string $imagePath = '', string $iconPath = '')  发送分享链接
 * @method textMsg(string $msg)    发送文本消息
 * @method revokeMsg(string $msgId)    发送文本消息
 * @property $eventToClass
 */
abstract class Robot implements SendEventInterface
{
    use MsgContent, MsgEvent, MsgUser, MsgSend, MsgType, MsgOther;

    protected string $url;
    protected string $token;

    abstract function __invoke($name, $args);

    public function __construct($m)
    {
        $this->msg = $m;
        if (method_exists($this, 'initParent')) {
            $this->initParent();
        }
    }

    abstract function initParent();

    /**
     * @const 文本消息
     */
    const TEXT = 1;
    /**
     * @const 图片消息
     */
    const IMAGE = 3;
    /**
     * @const 语音消息
     */
    const VOICE = 34;
    /**
     * @const 好友申请
     */
    const FRIEND_VERIFY = 37;

    /**
     * @const 名片消息
     */
    const CARD = 42;

    /**
     * @const 视频消息
     */
    const VIDEO = 43;

    /**
     * @const 表情消息
     */
    const GIF = 47;

    /**
     * @const 位置消息
     */
    const LOCATION = 48;

    /**
     * @const 分享链接
     */
    const SHARE_LINK = 47;

    /**
     * @const 转账消息
     */
    const TRANSFER = 2000;

    /**
     * @const 红包
     */
    const RED_PACKET = 2001;

    /**
     * @const 小程序消息
     */
    const MINI = 2002;

    /**
     * @const 群聊邀请消息
     */
    const GROUP_INVITE = 2003;

    /**
     * @const 收到文件消息
     */
    const FILE_RECEIVE = 2004;

    /**
     * @const 撤回消息
     */
    const RECALL = 2005;

    /**
     * @const 系统消息
     */
    const SYSTEM = 10000;

    /**
     * @const 服务消息
     */
    const SERVICE = 10002;

    /**
     * @var $msg array 消息本体
     */
    protected $msg;

    protected $log;

    /**
     * 更新日志
     * @param $data
     * @return void
     */
    public function updateLog($data = [])
    {
        $this->log->save($data);
    }

    /**
     * 获取机器人主人
     * @return array
     */
    public function getMasters()
    {
        return $this->masters;
    }

    /**
     * 发送消息至机器人主人
     * @param $msg
     * @return mixed
     */
    abstract public function sendToMasters($msg);

    /**
     * 判断是否是机器人主人
     * @return bool
     */
    public function isMaster()
    {
        foreach ($this->masters as $master) {
            if ($master->wxid == $this->memberWxid) {
                return true;
            }
        }
        return false;
    }

    public function __call($name, $arguments)
    {
        if (strpos($name, 'Msg') !== false) {
            $method = 'send' . ucfirst($name);
            if (strpos($name, 'Msg') && method_exists($this, $method)) {
                $data = $this->getBaseSendMsg($method);
                $data = array_merge($data, $this->$method($data, ...$arguments));
                if (isset($this->toGroup)) {
                    $this->changeToGroup($data);
                }
                if (isset($this->toFriend)) {
                    $this->changeToFriend($data);
                }
                return $this->send($data);
            }
        }
        throw new \Exception('暂不支持该方法：' . $name);
    }

    public function send($data)
    {
        $head = [];
        $res = HttpUtil::curl_post($this->url, $data, $head);
        return $res;
    }
}